package com.sdry.service.impl.llm;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sdry.mapper.hpf.AllReportMapper;
import com.sdry.mapper.hpf.PickApplyMapper;
import com.sdry.mapper.hpf.ProductMapper;
import com.sdry.mapper.llm.ProductionReportMapper;
import com.sdry.mapper.llm.TeamRecordMapper;
import com.sdry.model.hpf.ProductStatus;
import com.sdry.model.hpf.StationStatus;
import com.sdry.model.jyy.Cut;
import com.sdry.model.jyy.Shedudetail;
import com.sdry.model.jyy.Solid;
import com.sdry.model.llm.Team;
import com.sdry.model.llm.TeamRecord;
import com.sdry.model.workplace.Assemble;
import com.sdry.model.workplace.Centrifugal;
import com.sdry.model.workplace.MousseBundleSample;
import com.sdry.service.llm.ProductionReportService;

/**
 * 生产报表ServiceImp
 * @Title:ProductionReportServiceImp
 * @Package com.sdry.service.impl.llm
 * @author llm
 * @date 2018年12月24日
 * @version 1.0
 */

@Service
public class ProductionReportServiceImp implements ProductionReportService {

	@Resource
	private ProductionReportMapper productionReportMapper;
	@Resource
	private TeamRecordMapper teamRecordMapper;
	@Resource
	private AllReportMapper allReportMapper;
	@Resource
	private ProductMapper productMapper;
	@Resource
	private PickApplyMapper pickApplyMapper;
	
	/**    
	 * 生产报表   
	 * @param map
	 * @return    
	 */
	@Override
	public List<StationStatus> listAllProductionStatusByTimeAndTeam(Map<String, Object> map) {
		List<StationStatus> stationStatusList = new ArrayList<>();
		//1、根据班组名称查询班组ID
		Team team = productionReportMapper.getTeamByTeamName((String)map.get("teamName"));
		if(null != team){
			map.put("teamId", team.getTid());
			//2、根据日期和班组ID查询选择的排产单
			TeamRecord teamRecord = productionReportMapper.getTeamRecordByTimeAndTeamId(map);
			if(null != teamRecord){
				//3、根据排产单ID获取当天的计划生产量
				Shedudetail shedudetail = teamRecordMapper.getShedudetailById(Long.parseLong(teamRecord.getProductionId()));
				//构造方法的字符格式这里如果小数不足2位,会以0补足
				DecimalFormat decimalFormat=new DecimalFormat(".00");
				String[] stationName = {"膜丝", "组装", "离心", "固化", "切割"};
				if (null != shedudetail) {
					Object  dateNowStr = map.get("time");
					if("白班".equals(map.get("teamName"))){
						map.put("beginTime", dateNowStr+" 08:00:00");
						map.put("endTime", dateNowStr+" 20:00:00");
					}else{
						//晚班的工作时间，当天的20:00:00到下一天的08:00:00
						Calendar calendar = Calendar.getInstance();
						calendar.add(Calendar.DATE, +1);
						Date date = calendar.getTime();
						DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
						String nextDay = df.format(date);
						map.put("beginTime", dateNowStr+" 20:00:00");
						map.put("endTime", nextDay+" 08:00:00");
					}
					List<MousseBundleSample> msList = allReportMapper.listAllMSByTime(map);
					List<Assemble> zzList = allReportMapper.listAllZZByTime(map);
					List<Centrifugal> lxList = allReportMapper.listAllLXByTime(map);
					List<Solid> ghList = allReportMapper.listAllGHByTime(map);
					List<Cut> qgList = allReportMapper.listAllCutByTime(map);
					String b1 = shedudetail.getB1();//0960T 个数
					String c1 = shedudetail.getC1();//0980T	个数
					String d1 = shedudetail.getD1();//1080C	个数
					String e1 = shedudetail.getE1();//1060B 个数
					//计算膜丝工位每个产品的生产状况
					StationStatus stationStatus1 = new StationStatus();
					stationStatus1.setStationName(stationName[0]);
					List<ProductStatus> productStatusList1 = new ArrayList<>();
					ProductStatus productStatus11 = new ProductStatus();
					productStatus11.setPname("0960外压组件");
					int ms1 = 0;
					for (int i = 0; i < msList.size(); i++) {
						MousseBundleSample mousseBundleSample = msList.get(i);
						if ("01".equals(mousseBundleSample.getMbscode().substring(2, 4))) {
							ms1 ++;
						}
					}
					if ("".equals(b1) || b1 == null) {
						productStatus11.setPlanAmount(0);
						productStatus11.setLastPlanAmount(0 - ms1);
						productStatus11.setProductivity(0+"");
					} else {
						productStatus11.setPlanAmount((Integer.parseInt(b1)) * 24);
						productStatus11.setLastPlanAmount(((Integer.parseInt(b1)) * 24) - ms1);
						productStatus11.setProductivity(decimalFormat.format((double)ms1*100/(Integer.parseInt(b1) * 24))+"%");
					}
					productStatus11.setCompleteAmount(ms1);
					productStatusList1.add(productStatus11);
					ProductStatus productStatus12 = new ProductStatus();
					productStatus12.setPname("0980外压组件");
					int ms2 = 0;
					for (int i = 0; i < msList.size(); i++) {
						MousseBundleSample mousseBundleSample = msList.get(i);
						if ("02".equals(mousseBundleSample.getMbscode().substring(2, 4))) {
							ms2 ++;
						}
					}
					if ("".equals(c1) || c1 == null) {
						productStatus12.setPlanAmount(0);
						productStatus12.setLastPlanAmount(0 - ms2);
						productStatus12.setProductivity(0+"");
					} else {
						productStatus12.setPlanAmount((Integer.parseInt(c1)) * 24);
						productStatus12.setLastPlanAmount(((Integer.parseInt(c1)) * 24) - ms2);
						productStatus12.setProductivity(decimalFormat.format((double)ms2*100/(Integer.parseInt(c1) * 24))+"%");
					}
					productStatus12.setCompleteAmount(ms2);
					productStatusList1.add(productStatus12);
					ProductStatus productStatus13 = new ProductStatus();
					productStatus13.setPname("1080组件");
					int ms3 = 0;
					for (int i = 0; i < msList.size(); i++) {
						MousseBundleSample mousseBundleSample = msList.get(i);
						if ("04".equals(mousseBundleSample.getMbscode().substring(2, 4))) {
							ms3 ++;
						}
					}
					if ("".equals(d1) || d1 == null) {
						productStatus13.setPlanAmount(0);
						productStatus13.setLastPlanAmount(0 - ms3);
						productStatus13.setProductivity(0+"");
					} else {
						productStatus13.setPlanAmount((Integer.parseInt(d1)) * 32);
						productStatus13.setLastPlanAmount(((Integer.parseInt(d1)) * 32) - ms3);
						productStatus13.setProductivity(decimalFormat.format((double)ms3*100/(Integer.parseInt(d1) * 32))+"%");
					}
					productStatus13.setCompleteAmount(ms3);
					productStatusList1.add(productStatus13);
					ProductStatus productStatus14 = new ProductStatus();
					productStatus14.setPname("1060组件");
					int ms4 = 0;
					for (int i = 0; i < msList.size(); i++) {
						MousseBundleSample mousseBundleSample = msList.get(i);
						if ("03".equals(mousseBundleSample.getMbscode().substring(2, 4))) {
							ms4 ++;
						}
					}
					if ("".equals(e1) || e1 == null) {
						productStatus14.setPlanAmount(0);
						productStatus14.setLastPlanAmount(0 - ms4);
						productStatus14.setProductivity(0+"");
					} else {
						productStatus14.setPlanAmount((Integer.parseInt(e1)) * 32);
						productStatus14.setLastPlanAmount(((Integer.parseInt(e1)) * 32) - ms4);
						productStatus14.setProductivity(decimalFormat.format((double)ms4*100/(Integer.parseInt(e1) * 32))+"%");
					}
					productStatus14.setCompleteAmount(ms4);
					productStatusList1.add(productStatus14);
					stationStatus1.setProductStatusList(productStatusList1);
					stationStatusList.add(stationStatus1);
					//计算组装工位每个产品的生产状况
					StationStatus stationStatus2 = new StationStatus();
					stationStatus2.setStationName(stationName[1]);
					List<ProductStatus> productStatusList2 = new ArrayList<>();
					ProductStatus productStatus21 = new ProductStatus();
					productStatus21.setPname("0960外压组件");
					int zz1 = 0;
					for (int i = 0; i < zzList.size(); i++) {
						Assemble assemble = zzList.get(i);
						if ("0960".equals(assemble.getAtube().substring(3, 7))) {
							zz1 ++;
						}
					}
					if ("".equals(b1) || b1 == null) {
						productStatus21.setPlanAmount(0);
						productStatus21.setLastPlanAmount(0 - zz1);
						productStatus21.setProductivity(0+"");
					} else {
						productStatus21.setPlanAmount(Integer.parseInt(b1));
						productStatus21.setLastPlanAmount(Integer.parseInt(b1) - zz1/24);
						productStatus21.setProductivity(decimalFormat.format((double)zz1*100/24/Integer.parseInt(b1))+"%");
					}
					productStatus21.setCompleteAmount(zz1/24);
					productStatusList2.add(productStatus21);
					ProductStatus productStatus22 = new ProductStatus();
					productStatus22.setPname("0980外压组件");
					int zz2 = 0;
					for (int i = 0; i < zzList.size(); i++) {
						Assemble assemble = zzList.get(i);
						if ("0980".equals(assemble.getAtube().substring(3, 7))) {
							zz2 ++;
						}
					}
					if ("".equals(c1) || c1 == null) {
						productStatus22.setPlanAmount(0);
						productStatus22.setLastPlanAmount(0 - zz2);
						productStatus22.setProductivity(0+"");
					} else {
						productStatus22.setPlanAmount(Integer.parseInt(c1));
						productStatus22.setLastPlanAmount(Integer.parseInt(c1) - zz2/24);
						productStatus22.setProductivity(decimalFormat.format((double)zz2*100/24/Integer.parseInt(c1))+"%");
					}
					productStatus22.setCompleteAmount(zz2/24);
					productStatusList2.add(productStatus22);
					ProductStatus productStatus23 = new ProductStatus();
					productStatus23.setPname("1080组件");
					int zz3 = 0;
					for (int i = 0; i < zzList.size(); i++) {
						Assemble assemble = zzList.get(i);
						if ("1080".equals(assemble.getAtube().substring(3, 7))) {
							zz3 ++;
						}
					}
					if ("".equals(d1) || d1 == null) {
						productStatus23.setPlanAmount(0);
						productStatus23.setLastPlanAmount(0 - zz3);
						productStatus23.setProductivity(0+"");
					} else {
						productStatus23.setPlanAmount(Integer.parseInt(d1));
						productStatus23.setLastPlanAmount(Integer.parseInt(d1) - zz3/32);
						productStatus23.setProductivity(decimalFormat.format((double)zz3*100/32/Integer.parseInt(d1))+"%");
					}
					productStatus23.setCompleteAmount(zz3/32);
					productStatusList2.add(productStatus23);
					ProductStatus productStatus24 = new ProductStatus();
					productStatus24.setPname("1060组件");
					int zz4 = 0;
					for (int i = 0; i < zzList.size(); i++) {
						Assemble assemble = zzList.get(i);
						if ("1060".equals(assemble.getAtube().substring(3, 7))) {
							zz4 ++;
						}
					}
					if ("".equals(e1) || e1 == null) {
						productStatus24.setPlanAmount(0);
						productStatus24.setLastPlanAmount(0 - zz4);
						productStatus24.setProductivity(0+"");
					} else {
						productStatus24.setPlanAmount(Integer.parseInt(e1));
						productStatus24.setLastPlanAmount(Integer.parseInt(e1) - zz4/32);
						productStatus24.setProductivity(decimalFormat.format((double)zz4*100/32/Integer.parseInt(e1))+"%");
					}
					productStatus24.setCompleteAmount(zz4/24);
					productStatusList2.add(productStatus24);
					stationStatus2.setProductStatusList(productStatusList2);
					stationStatusList.add(stationStatus2);
					//计算离心工位每个产品的生产状况
					StationStatus stationStatus3 = new StationStatus();
					stationStatus3.setStationName(stationName[2]);
					List<ProductStatus> productStatusList3 = new ArrayList<>();
					ProductStatus productStatus31 = new ProductStatus();
					productStatus31.setPname("0960外压组件");
					int lx1 = 0;
					for (int i = 0; i < lxList.size(); i++) {
						Centrifugal centrifugal = lxList.get(i);
						if ("0960".equals(centrifugal.getCtube().substring(3, 7))) {
							lx1 ++;
						}
					}
					if ("".equals(b1) || b1 == null) {
						productStatus31.setPlanAmount(0);
						productStatus31.setLastPlanAmount(0 - lx1);
						productStatus31.setProductivity(0+"");
					} else {
						productStatus31.setPlanAmount(Integer.parseInt(b1));
						productStatus31.setLastPlanAmount(Integer.parseInt(b1) - lx1);
						productStatus31.setProductivity(decimalFormat.format((double)lx1*100/Integer.parseInt(b1))+"%");
					}
					productStatus31.setCompleteAmount(lx1);
					productStatusList3.add(productStatus31);
					ProductStatus productStatus32 = new ProductStatus();
					productStatus32.setPname("0980外压组件");
					int lx2 = 0;
					for (int i = 0; i < lxList.size(); i++) {
						Centrifugal centrifugal = lxList.get(i);
						if ("0980".equals(centrifugal.getCtube().substring(3, 7))) {
							lx2 ++;
						}
					}
					if ("".equals(c1) || c1 == null) {
						productStatus32.setPlanAmount(0);
						productStatus32.setLastPlanAmount(0 - lx2);
						productStatus32.setProductivity(0+"");
					} else {
						productStatus32.setPlanAmount(Integer.parseInt(c1));
						productStatus32.setLastPlanAmount(Integer.parseInt(c1) - lx2);
						productStatus32.setProductivity(decimalFormat.format((double)lx2*100/Integer.parseInt(c1))+"%");
					}
					productStatus32.setCompleteAmount(lx2);
					productStatusList3.add(productStatus32);
					ProductStatus productStatus33 = new ProductStatus();
					productStatus33.setPname("1080组件");
					int lx3 = 0;
					for (int i = 0; i < lxList.size(); i++) {
						Centrifugal centrifugal = lxList.get(i);
						if ("1080".equals(centrifugal.getCtube().substring(3, 7))) {
							lx3 ++;
						}
					}
					if ("".equals(d1) || d1 == null) {
						productStatus33.setPlanAmount(0);
						productStatus33.setLastPlanAmount(0 - lx3);
						productStatus33.setProductivity(0+"");
					} else {
						productStatus33.setPlanAmount(Integer.parseInt(d1));
						productStatus33.setLastPlanAmount(Integer.parseInt(d1) - lx3);
						productStatus33.setProductivity(decimalFormat.format((double)lx3*100/Integer.parseInt(d1))+"%");
					}
					productStatus33.setCompleteAmount(lx3);
					productStatusList3.add(productStatus33);
					ProductStatus productStatus34 = new ProductStatus();
					productStatus34.setPname("1060组件");
					int lx4 = 0;
					for (int i = 0; i < lxList.size(); i++) {
						Centrifugal centrifugal = lxList.get(i);
						if ("1060".equals(centrifugal.getCtube().substring(3, 7))) {
							lx4 ++;
						}
					}
					if ("".equals(e1) || e1 == null) {
						productStatus34.setPlanAmount(0);
						productStatus34.setLastPlanAmount(0 -lx4);
						productStatus34.setProductivity(0+"");
					} else {
						productStatus34.setPlanAmount(Integer.parseInt(e1));
						productStatus34.setLastPlanAmount(Integer.parseInt(e1) - lx4);
						productStatus34.setProductivity(decimalFormat.format((double)lx4*100/Integer.parseInt(e1))+"%");
					}
					productStatus34.setCompleteAmount(lx4);
					productStatusList3.add(productStatus34);
					stationStatus3.setProductStatusList(productStatusList3);
					stationStatusList.add(stationStatus3);
					//计算固化工位每个产品的生产状况
					StationStatus stationStatus4 = new StationStatus();
					stationStatus4.setStationName(stationName[3]);
					List<ProductStatus> productStatusList4 = new ArrayList<>();
					ProductStatus productStatus41 = new ProductStatus();
					productStatus41.setPname("0960外压组件");
					int gh1 = 0;
					for (int i = 0; i < ghList.size(); i++) {
						Solid solid = ghList.get(i);
						if ("0960".equals(solid.getStube().substring(3, 7))) {
							gh1 ++;
						}
					}
					if ("".equals(b1) || b1 == null) {
						productStatus41.setPlanAmount(0);
						productStatus41.setLastPlanAmount(0 - gh1);
						productStatus41.setProductivity(0+"");
					} else {
						productStatus41.setPlanAmount(Integer.parseInt(b1));
						productStatus41.setLastPlanAmount(Integer.parseInt(b1) - gh1);
						productStatus41.setProductivity(decimalFormat.format((double)gh1*100/Integer.parseInt(b1))+"%");
					}
					productStatus41.setCompleteAmount(gh1);
					productStatusList4.add(productStatus41);
					ProductStatus productStatus42 = new ProductStatus();
					productStatus42.setPname("0980外压组件");
					int gh2 = 0;
					for (int i = 0; i < ghList.size(); i++) {
						Solid solid = ghList.get(i);
						if ("0980".equals(solid.getStube().substring(3, 7))) {
							gh2 ++;
						}
					}
					if ("".equals(c1) || c1 == null) {
						productStatus42.setPlanAmount(0);
						productStatus42.setLastPlanAmount(0 - gh2);
						productStatus42.setProductivity(0+"");
					} else {
						productStatus42.setPlanAmount(Integer.parseInt(c1));
						productStatus42.setLastPlanAmount(Integer.parseInt(c1) - gh2);
						productStatus42.setProductivity(decimalFormat.format((double)gh2*100/Integer.parseInt(c1))+"%");
					}
					productStatus42.setCompleteAmount(gh2);
					productStatusList4.add(productStatus42);
					ProductStatus productStatus43 = new ProductStatus();
					productStatus43.setPname("1080组件");
					int gh3 = 0;
					for (int i = 0; i < ghList.size(); i++) {
						Solid solid = ghList.get(i);
						if ("1080".equals(solid.getStube().substring(3, 7))) {
							gh3 ++;
						}
					}
					if ("".equals(d1) || d1 == null) {
						productStatus43.setPlanAmount(0);
						productStatus43.setLastPlanAmount(0 - gh3);
						productStatus43.setProductivity(0+"");
					} else {
						productStatus43.setPlanAmount(Integer.parseInt(d1));
						productStatus43.setLastPlanAmount(Integer.parseInt(d1) - gh3);
						productStatus43.setProductivity(decimalFormat.format((double)gh3*100/Integer.parseInt(d1))+"%");
					}
					productStatus43.setCompleteAmount(gh3);
					productStatusList4.add(productStatus43);
					ProductStatus productStatus44 = new ProductStatus();
					productStatus44.setPname("1060组件");
					int gh4 = 0;
					for (int i = 0; i < ghList.size(); i++) {
						Solid solid = ghList.get(i);
						if ("1060".equals(solid.getStube().substring(3, 7))) {
							gh4 ++;
						}
					}
					if ("".equals(e1) || e1 == null) {
						productStatus44.setPlanAmount(0);
						productStatus44.setLastPlanAmount(0 - gh4);
						productStatus44.setProductivity(0+"");
					} else {
						productStatus44.setPlanAmount(Integer.parseInt(e1));
						productStatus44.setLastPlanAmount(Integer.parseInt(e1) - gh4);
						productStatus44.setProductivity(decimalFormat.format((double)gh4*100/Integer.parseInt(e1))+"%");
					}
					productStatus44.setCompleteAmount(gh4);
					productStatusList4.add(productStatus44);
					stationStatus4.setProductStatusList(productStatusList4);
					stationStatusList.add(stationStatus4);
					//计算切割工位每个产品的生产状况
					StationStatus stationStatus5 = new StationStatus();
					stationStatus5.setStationName(stationName[4]);
					List<ProductStatus> productStatusList5 = new ArrayList<>();
					ProductStatus productStatus51 = new ProductStatus();
					productStatus51.setPname("0960外压组件");
					int qg1 = 0;
					for (int i = 0; i < qgList.size(); i++) {
						Cut cut = qgList.get(i);
						if ("0960".equals(cut.getCtube().substring(3, 7))) {
							qg1 ++;
						}
					}
					if ("".equals(b1) || b1 == null) {
						productStatus51.setPlanAmount(0);
						productStatus51.setLastPlanAmount(0 - qg1);
						productStatus51.setProductivity(0+"");
					} else {
						productStatus51.setPlanAmount(Integer.parseInt(b1));
						productStatus51.setLastPlanAmount(Integer.parseInt(b1) - qg1);
						productStatus51.setProductivity(decimalFormat.format((double)qg1*100/Integer.parseInt(b1))+"%");
					}
					productStatus51.setCompleteAmount(qg1);
					productStatusList5.add(productStatus51);
					ProductStatus productStatus52 = new ProductStatus();
					productStatus52.setPname("0980外压组件");
					int qg2 = 0;
					for (int i = 0; i < qgList.size(); i++) {
						Cut cut = qgList.get(i);
						if ("0980".equals(cut.getCtube().substring(3, 7))) {
							qg2 ++;
						}
					}
					if ("".equals(c1) || c1 == null) {
						productStatus52.setPlanAmount(0);
						productStatus52.setLastPlanAmount(0 - qg2);
						productStatus52.setProductivity(0+"");
					} else {
						productStatus52.setPlanAmount(Integer.parseInt(c1));
						productStatus52.setLastPlanAmount(Integer.parseInt(c1) - qg2);
						productStatus52.setProductivity(decimalFormat.format((double)qg2*100/Integer.parseInt(c1))+"%");
					}
					productStatus52.setCompleteAmount(qg2);
					productStatusList5.add(productStatus52);
					ProductStatus productStatus53 = new ProductStatus();
					productStatus53.setPname("1080组件");
					int qg3 = 0;
					for (int i = 0; i < qgList.size(); i++) {
						Cut cut = qgList.get(i);
						if ("1080".equals(cut.getCtube().substring(3, 7))) {
							qg3 ++;
						}
					}
					if ("".equals(d1) || d1 == null) {
						productStatus53.setPlanAmount(0);
						productStatus53.setLastPlanAmount(0 - qg3);
						productStatus53.setProductivity(0+"");
					} else {
						productStatus53.setPlanAmount(Integer.parseInt(d1));
						productStatus53.setLastPlanAmount(Integer.parseInt(d1) - qg3);
						productStatus53.setProductivity(decimalFormat.format((double)qg3*100/Integer.parseInt(d1))+"%");
					}
					productStatus53.setCompleteAmount(qg3);
					productStatusList5.add(productStatus53);
					ProductStatus productStatus54 = new ProductStatus();
					productStatus54.setPname("1060组件");
					int qg4 = 0;
					for (int i = 0; i < qgList.size(); i++) {
						Cut cut = qgList.get(i);
						if ("1060".equals(cut.getCtube().substring(3, 7))) {
							qg4 ++;
						}
					}
					productStatus54.setCompleteAmount(qg4);
					if ("".equals(e1) || e1 == null) {
						productStatus54.setPlanAmount(0);
						productStatus54.setLastPlanAmount(0 - qg4);
						if(productStatus54.getCompleteAmount() > 0){
							productStatus54.setProductivity("100%");
						}else{
							productStatus54.setProductivity(0+"");
						}
					} else {
						productStatus54.setPlanAmount(Integer.parseInt(e1));
						productStatus54.setLastPlanAmount(Integer.parseInt(e1) - qg4);
						productStatus54.setProductivity(decimalFormat.format((double)qg4*100/Integer.parseInt(e1))+"%");
					}
					productStatusList5.add(productStatus54);
					stationStatus5.setProductStatusList(productStatusList5);
					stationStatusList.add(stationStatus5);
					return stationStatusList;
				} else {
					//排产单不存在
					return stationStatusList;
				}
			}else {
				//排产单不存在
				return stationStatusList;
			}
		}
		return null;
	}

}
